package lucius.work.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import lucius.work.entity.Book;
import lucius.work.service.BookService;

@Controller
@RequestMapping("/book")
public class BookController {

	/*
	 * A 是接口
	 * B implements A
	 * 相当于 A a = new B();
	 */
	@Resource(name="bookService")
	BookService bookService;
	
	//注入页容量
	@Value("${bookPageSize}")
	private String bookPageSize;
	


	public String getBookPageSize() {
		return bookPageSize;
	}

	public void setBookPageSize(String bookPageSize) {
		this.bookPageSize = bookPageSize;
	}

	@RequestMapping("/add.do")
	public ModelAndView add(Book book) throws Exception {

		//过去页面传来的参数
		Integer book_id = book.getBook_id();
		String book_name = book.getBook_name();
		String author = book.getAuthor();
		String publish = book.getPublish();
		Integer price = book.getPrice();
		String type = book.getType();
		Integer num = book.getNum();
		String status = "1";
		String place = book.getPlace();
		
		//封装成book对象
		Book b = new Book();
		b.setBook_id(book_id);
		b.setBook_name(book_name);
		b.setAuthor(author);
		b.setPublish(publish);
		b.setPrice(price);
		b.setType(type);
		b.setNum(num);
		b.setStatus(status);
		b.setPlace(place);
		//使用BaseServiceImpl的add方法
		bookService.add(b);
		
		return new ModelAndView("index");

	}
	
	@RequestMapping("/listBook.do")
	public ModelAndView listBook(String page,HttpServletRequest request) {

		System.out.println(page);
		Map<String, Object> data = new HashMap<String, Object>();
		// 每页显示的条数
		int pageSize = Integer.valueOf(getBookPageSize());

		// 查到的图书总数
		List<book> list = bookService.findAll(book.class);
		data.put("bookNum", list.size());

		// 总页数
		int totalPage = bookService.totalPage(pageSize);
		data.put("totalPage", totalPage);
		request.setAttribute("totalPage", totalPage);

		// 页面初始的时候page没有值
		if (page == null) {
			page = "1";
		}

		// 每页开始的第几条记录
		int startRow = (Integer.parseInt(page) - 1) * pageSize;
		list = bookService.books(startRow, pageSize);
		data.put("currentPage", Integer.valueOf(page));
		data.put("bookList", list);
		return new ModelAndView("html/bookList",data);
	}
	
	@RequestMapping("/findAll.do")
	public ModelAndView findAll() throws Exception{
		
		//使用BaseServiceImpl的findAll方法
		List<Book> list = bookService.findAll(Book.class);
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("bookList", list);
		
		return new ModelAndView("html/bookList",data);
	}
	
	@RequestMapping("/toUpdate.do")
	public ModelAndView toUpdate(ServletRequest request)throws Exception{
		
		String book_id = request.getParameter("name");
		//获取book_id对应的book对象
		Book b = bookService.getById(Book.class, Integer.parseInt(book_id));
		//填充data
		Map<String,Object> data = new HashMap<String,Object>(); 
		data.put("b", b);
		
		return new ModelAndView("html/updateBook",data);
	}
	
	@RequestMapping("/update.do")
	public ModelAndView update(Book book) throws Exception {

		// 过去页面传来的参数
		Integer book_id = book.getBook_id();
		String book_name = book.getBook_name();
		String author = book.getAuthor();
		String publish = book.getPublish();
		Integer price = book.getPrice();
		String type = book.getType();
		Integer num = book.getNum();
		String status = "1";
		String place = book.getPlace();

		// 封装成book对象
		Book b = new Book();
		b.setBook_id(book_id);
		b.setBook_name(book_name);
		b.setAuthor(author);
		b.setPublish(publish);
		b.setPrice(price);
		b.setType(type);
		b.setNum(num);
		b.setStatus(status);
		b.setPlace(place);
		// 使用BaseServiceImpl的update方法
		bookService.update(b);
		// 使用BaseServiceImpl的findAll方法
		List<book> list = bookService.findAll(book.class);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("bookList", list);
		
		return new ModelAndView("html/bookList",data);
	}

	@RequestMapping("/list.do")
	public ModelAndView list(){
		return new ModelAndView("html/addBook");
	}

}
